﻿using System;
using System.ComponentModel;
using System.Windows;

namespace WiiChuckDemo
{
    /// <summary>
    /// Interaction logic for WiiChuckView.xaml
    /// </summary>
    public partial class WiiChuckView
    {
        private readonly WiiChuckModel _model;

        public WiiChuckView()
        {
            InitializeComponent();

            _model = new WiiChuckModel();
            _model.DataRecordReceived += WiiChuckModel_DataRecordReceived;
            DataContext = _model;

            Loaded += WiiChuckView_Loaded;
        }

        void WiiChuckModel_DataRecordReceived(object sender, EventArgs e)
        {
        }

        void WiiChuckView_Loaded(object sender, RoutedEventArgs e)
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
                _model.OpenSerial();
        }

        private void ButtonSnapshot_Click(object sender, RoutedEventArgs e)
        {
            var avgAccelX = _model.AccelerationX.Average(10);
            var avgAccelY = _model.AccelerationY.Average(10);
            var avgAccelZ = _model.AccelerationZ.Average(10);

            string line = string.Format("AccelX: {0:F2} | AccelY: {1:F2} | Accel: Z {2:F2}",
                avgAccelX, avgAccelY, avgAccelZ);

            logTextBox.Text += line + Environment.NewLine;
        }
    }
}
